


/*
Filename: A5_DefaultProb.do
Goal: Calculate expected default probabilities for publicly traded firms following the methodology of Keswani, Tran, and Volpin (2021).
Contact: mjha@gsu.edu (author) and gormley@wustl.edu

Data Sources
All datasets listed below are proprietary and accessible exclusively through WRDS.
1. Equity Market Data
File: crsp_return.dta
Source Path: WRDS → CRSP Annual Update → Stock/Security Files → Daily Stock File
Description: Daily stock returns and pricing data

2. Fundamental Financial Data
File: compu_annual.dta
Source Path: WRDS → Compustat Capital IQ → Compustat → North America Fundamentals Annual
Description: Annual financial statement data
*/


capture log close
capture restore
set more off
clear all


global firm = "permco" //cik


****** start with CRSP annual return
use "crsp_return_pseudo.dta", clear
gen year=year(date)


// add permco
rename CUSIP cusip_all
merge m:1 cusip_all using "Cusip_Permco.dta" 
drop if _merge < 3
drop if missing(permco)

gsort $firm date
quietly by $firm date:  gen dup = cond(_N==1,0,_n)
drop if dup > 0
drop dup

// annualized return
gsort $firm date
gen k=1+RET
gen lk=ln(k)
egen s=sum(lk), by($firm year)
gen anret=exp(s)-1

egen sd=sd(lk), by($firm year)
gsort $firm year
quietly by $firm year:  gen dup = cond(_N==1,0,_n)
drop if dup > 1

keep $firm year anret sd
save "crsp_$firm.dta", replace



*******
use "compu_annual_pseudo", clear
keep if indfmt=="INDL" & datafmt=="STD" & popsrc=="D" & consol=="C"

count if missing(fyear)
gen tempyear = yofd(datadate)
replace fyear = tempyear if missing(fyear)
rename fyear year

replace csho = 0 if missing(csho)
replace dlc = 0 if missing(dlc)
replace dltt = 0 if missing(dltt)
replace prcc_f = 0 if missing(prcc_f)
   

// add permco/ cik
gen permco_temp = .
foreach var in 9 8 { // we start with most rigourous first
	gen cusip_all = substr(cusip,1,`var')
	merge m:1 cusip_all using "Cusip_Permco.dta"
	drop if _merge == 2
	drop _merge
	replace permco_temp = permco if missing(permco_temp)
	drop permco cusip_all
}
rename permco_temp permco
drop if missing(permco)


gsort $firm year -csho
quietly by $firm year:  gen dup = cond(_N==1,0,_n)
drop if dup > 1
drop dup
egen firm_year = group($firm year)

gen debt=dlc+(0.5*dltt)
gen equity= prcc_f*csho   

// merge
merge 1:1 $firm year using "crsp_$firm.dta"
drop if _merge < 3
drop _merge

tsset $firm year
gen lret=l1.anret
gen lsd = l1.sd 

gen avol=((equity/(equity+debt))*lsd)+((debt/(equity+debt))*(0.05+(0.25*lsd)))
gen dd=(log((equity+debt)/debt)+(lret-((avol*avol)/2)))/(avol)

replace dd=-1*dd
gen edf=normal(dd)

drop if missing(edf)
gsort year
by year: summarize edf 

gsort $firm year
quietly by $firm year:  gen dup = cond(_N==1,0,_n)
drop if dup > 0

keep $firm year edf 
save "edf_$firm.dta", replace
